package com.wh1200.mybatis.xmlless.model

import com.wh1200.mybatis.xmlless.model.base.BaseClassWrapper
import com.wh1200.mybatis.xmlless.model.exception.BuildSQLException
import com.wh1200.mybatis.xmlless.resolver.MappingResolver
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

/**
 * @author 吴昊
 * @date 2024/1/18 14:48
 * @since v0.0.0
 * @version 1.0
 */
class TypeInfo(val type: Type, val realType: BaseClassWrapper) {

  /** 返回join属性的原始类型 */
  val rawType: Class<*>
    get() {
      return when (type) {
        is Class<*>          -> type
        is ParameterizedType -> type.rawType as Class<*>
        else                 -> throw BuildSQLException("无法识别的类型：$type")
      }
    }

  fun getMappings(): FieldMappingList? {
    return MappingResolver.getMappingCache(realType.getClassName())
  }

  fun isComplexType(): Boolean {
    return realType.isComplex()
  }

}
